package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.base.util.UUIDUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.userinformation.UserInformationCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.userinformation.UserInformationCreateForm;
import com.bwda.dsrs.systemservice.domain.form.userinformation.UserInformationQueryForm;
import com.bwda.dsrs.systemservice.domain.form.userinformation.UserInformationUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.userinformation.UserInformationPo;
import com.bwda.dsrs.systemservice.domain.vo.userinformation.UserInformationVo;
import com.bwda.dsrs.systemservice.service.UserInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yangjun
 */
@RestController
@RequestMapping("/s/userinformation")
@Api(value = "/s/userinformation", tags = "用户详情表")
public class UserInformationController extends BaseController {

    @Autowired
    private UserInformationService userInformationService;

    @ApiOperation(value = "查询用户详情表", notes = "查询用户详情表", httpMethod = "GET")
    @Loggable(descp = "查询用户详情表", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<UserInformationVo> query(@ApiParam(value = "用户编码", required = true) @RequestParam String userCode) throws SystemServiceException {
        UserInformationPo po = userInformationService.queryWithValid(userCode);
        UserInformationVo vo = CopyUtil.transfer(po, UserInformationVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询用户详情表数量", notes = "查询用户详情表数量", httpMethod = "GET")
    @Loggable(descp = "查询用户详情表数量", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid UserInformationQueryForm form) throws SystemServiceException {
        UserInformationCondition condition = this.getConditionByQueryForm(form);
        int count = userInformationService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询用户详情表列表", notes = "查询用户详情表列表", httpMethod = "GET")
    @Loggable(descp = "查询用户详情表列表", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<UserInformationVo>> queryList(@ModelAttribute @Valid UserInformationQueryForm form) throws SystemServiceException {
        UserInformationCondition condition = this.getConditionByQueryForm(form);
        List<UserInformationPo> poList = userInformationService.queryList(condition);
        List<UserInformationVo> voList = CopyUtil.transfer(poList, UserInformationVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询用户详情表列表(带分页)", notes = "查询用户详情表列表(带分页)", httpMethod = "GET")
    @Loggable(descp = "查询用户详情表列表(带分页)", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<UserInformationVo>> queryPageList(@ModelAttribute @Valid UserInformationQueryForm form) throws SystemServiceException {
        UserInformationCondition condition = this.getConditionByQueryForm(form);
        List<UserInformationVo> voList = new ArrayList<>();
        int count = userInformationService.queryCount(condition);
        if (count > 0) {
            List<UserInformationPo> poList = userInformationService.queryList(condition);
            voList = CopyUtil.transfer(poList, UserInformationVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "新增用户详情表", notes = "新增用户详情表", httpMethod = "POST")
    @Loggable(descp = "新增用户详情表", type = LogTypeEnum.CREATE, include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<UserInformationVo> add(@ModelAttribute @Valid UserInformationCreateForm form) throws SystemServiceException {
        UserInformationPo po = CopyUtil.transfer(form, UserInformationPo.class);
        po.setUserCode(UUIDUtil.getUuid());
        userInformationService.insert(po);
        UserInformationVo vo = CopyUtil.transfer(po, UserInformationVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改用户详情表", notes = "修改用户详情表", httpMethod = "POST")
    @Loggable(descp = "修改用户详情表", type = LogTypeEnum.UPDATE, include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid UserInformationUpdateForm form) throws SystemServiceException {
        UserInformationPo po = CopyUtil.transfer(form, UserInformationPo.class);
        userInformationService.update(po);
        return getSuccessResult();
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除用户详情表", notes = "删除用户详情表", httpMethod = "POST")
    @Loggable(descp = "删除用户详情表", type = LogTypeEnum.DELETE, include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "用户编码", required = true) @RequestParam String userCode) throws SystemServiceException {
        userInformationService.delete(userCode);
        return getSuccessResult();
    }

    /**
     * UserInformationQueryForm转换为UserInformationCondition
     *
     * @param form
     * @return
     */
    private UserInformationCondition getConditionByQueryForm(UserInformationQueryForm form) {
        UserInformationCondition condition = CopyUtil.transfer(form, UserInformationCondition.class);
        return condition;
    }

}